﻿$(document).ready(function () {
    var forumId = $('#Id').val();

    initTopicTypeAdd();
    initTopicTypeList(forumId);
})

function initTopicTypeAdd() {
    var typesTableStr ="<div>"
    typesTableStr += "<input id='addNewType' type='button' value='添加' onClick='showAddNewTopicType()'>";
    typesTableStr += "<input id='hidenNewType' type='button' value='隐藏' style='display:none' onClick='hideAddNewTopicType()'>";

    typesTableStr += "<div id='newTypeAdd' class=\"formItem floatContainer\" style='display:none'>";
    typesTableStr += "<label>类别名称</label>";
    typesTableStr += "<input id=\"newTypeName\" type=\"text\" value=\"\">";
    typesTableStr += "<input type='button' value='添加' onClick='addNewTopicType()'></div></div>";

    $("#topicTypeAdd").html(typesTableStr);
}

function initTopicTypeList(num) {
    $.get(AC_ROOT_DIR + "/admin/forums/GetTopicType?forumID=" + num, function (result) {
        var jsonObj = eval("(" + result + ")"); //将JSON文本转换为对象，JSON文本必须括在括号中避免产生JavaScript的语法歧义。

        var typesTableStr = "<table><thead><tr>"
        typesTableStr += "<th>序号</th>";
        typesTableStr += "<th>类别名称</th>";
        typesTableStr += "<th style='display:none'>简写</th>";
        typesTableStr += "</tr></thead>";
        typesTableStr += "<tbody>";

        for (var i = 0; i < jsonObj.length; i++) {
            typesTableStr += "<tr>";
            typesTableStr += "<td>" + jsonObj[i][0] + "</td>";
            typesTableStr += "<td><input id='" + jsonObj[i][0] + "' type='text' style='readonly:readonly' onClick='showEdit(" + jsonObj[i][0] + ")' value='" + jsonObj[i][1] + "'></td>";
            typesTableStr += "<td style='display:none'>" + jsonObj[i][2] + "</td>";
//            typesTableStr += "<td><input id='del_" + jsonObj[i][0] + "' type='button' value='删 除' onClick='delTopicType(" + jsonObj[i][0] + ")'></td>";
            typesTableStr += "<td><input id='edit_" + jsonObj[i][0] + "' type='button' value='更 新' onClick='editTypeName(" + jsonObj[i][0] + ")'></td>";
            typesTableStr += "</tr>";
        }

        typesTableStr += "</tbody></table>";

        $("#topicTypeList").html(typesTableStr);
    });
}

 function showAddNewTopicType() {
    $("#newTypeAdd").show();
    $("#hidenNewType").show();
    $("#addNewType").hide();
 }

 function hideAddNewTopicType() {
     $("#hidenNewType").hide();
     $("#addNewType").show();
     $("#newTypeAdd").hide();
 }

 function addNewTopicType() {

     var forumId = $('#Id').val();
     var typeName = $('#newTypeName').val();

     $.ajax({
         type: "POST",
         url: AC_ROOT_DIR + "/admin/forums/AddTopicType",
         data: "&forumId=" + forumId + "+ &typeName=" + typeName,
         dataType: "text",
         cache: false,
         success: function (result) {
             var jsonResult = eval(result);
             initTopicTypeList(forumId);
         }
     });
 }

 function showEdit(topicTypeID) {
//     $("#del_" + topicTypeID).hide();
//     $("#edit_" + topicTypeID).show();
//     $("#" + topicTypeID).attr("readonly") = false;

 }

 function delTopicType(topicTypeID) {

 }

 function editTypeName(topicTypeId) {
//     $("#del_" + topicTypeID).show();
//     $("#edit_" + topicTypeID).hide();
     var typeName = $("#" + topicTypeId).val();

     $.ajax({
         type: "POST",
         url: AC_ROOT_DIR + "/admin/forums/EditTopicType",
         data: "&topicTypeId=" + topicTypeId + "+ &typeName=" + typeName,
         dataType: "text",
         cache: false,
         success: function (result) {
             var jsonResult = eval(result);
             initTopicTypeList(forumId);
         }
     });
 }

